package elearning.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import elearning.model.ToeicCategory;


@Repository
public class CategoryDao {

	@PersistenceContext
	private EntityManager em;
	
	@Transactional
	public List<ToeicCategory> queryAll() {
        Query query = em.createQuery("SELECT t FROM ToeicCategory t");
        List<ToeicCategory> result = (List<ToeicCategory>)query.getResultList();
        return result;
    }
	@Transactional
    public ToeicCategory get(Integer id){
        return em.find(ToeicCategory.class,id);
    }
	@Transactional
    public ToeicCategory save(ToeicCategory category){
        try {
			em.persist(category);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return category;
    }
	@Transactional
    public ToeicCategory update(ToeicCategory category){
    	category = em.merge(category);
        return category;
    }
}
